package edu.mju.util;

import java.util.List;

/**
 * @Author:wyp
 * @Date:2020/5/11 16:12
 */
public class Pager {
    /**
     * 当前页
     */
    private int page = 1;
    /**
     * 页面大小
     */
    private int pageSize = 10;
    /**
     * 总行数
     */
    private int rows;
    /**
     * 总页数：只读
     */
    private int pages;
    /**
     * 是否是第一页：只读
     */
    private boolean first;
    /**
     * 是否是最后一页：只读
     */
    private  boolean last;
    /**
     * 上一页：只读
     */
    private int prev;
    /**
     * 下一页：只读
     */
    private int next;
    /**
     * 存放数据
     */
    private List<Object> data;

    public Pager() {
    }

    public Pager(int page, int pageSize) {
        setPage(page);
        setPageSize(pageSize);
    }

    //get set方法

    public int getPage() {

        getPages();
        System.out.println("getPage:"+page+",pages:"+pages);
        if(page>pages && pages>-1){
            page = pages;
        }
        if(page < 1){
            page = 1;
        }
        return page;
    }

    public void setPage(int page) {
        if(page < 1){
            page = 1;
        }
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if(pageSize < 1){
            pageSize = 10;
        }
        this.pageSize = pageSize;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if(rows>=0){
            this.rows = rows;
        }
    }

    public int getPages() {
        pages = rows/pageSize;
        if(pages*pageSize<rows){
            pages = pages+1;
        }
        if(pages < 1){
            pages = 1;
        }

        return pages;
    }

    public boolean isFirst() {
        if(page == 1){
            return true;
        }else {
            return false;
        }
    }

    public boolean isLast() {
        getPages();
        if(page == pages){
            return true;
        }else {
            return false;
        }

    }

    public int getPrev() {
        prev = page - 1;
        if(prev <1){
            prev = 1;
        }
        return prev;
    }

    public int getNext() {
        getPages();
        next = page + 1;
        if(next>pages){
            next = pages;
        }
        return next;
    }

    public List<Object> getData() {
        return data;
    }

    public void setData(List<Object> data) {
        this.data = data;
    }
}
